package anc.server.tankshooter.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import anc.server.tankshooter.App;
import anc.server.tankshooter.models.Account;
import anc.server.tankshooter.models.IEntity;
import anc.server.tankshooter.models.Match;

/**
 * 
 * @author tauitdnmd
 *
 */
public class MatchManager extends IManager {

	public MatchManager(Connection connection) {
		super(connection, Connect.MATCH_TABLE);
		
	}

	@Override
	public int add(IEntity entity) {
		int newId = -1;
		try{
			Match match = (Match)entity;
			String sql ="INSERT INTO "+getTableName()+" VALUES(null,?,?,?,?,?,?,?,?)";
			PreparedStatement statement = mConnection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			statement.setInt(1, match.getOwnerId());
			statement.setInt(2, match.getCompetitorId());
			statement.setInt(3, match.getWinnerId());
			statement.setInt(4, match.getMapId());
			statement.setInt(5, match.getMapId());
			statement.setInt(6, match.getStartTime());
			statement.setInt(7, match.getEndTime());
			statement.setInt(8, match.getStatus());
			
			statement.execute();
			
			if(statement.getUpdateCount()>0){
				ResultSet reader = statement.getGeneratedKeys();
				if(reader.next()){
					newId = reader.getInt(1);
				}
				reader.close();
			}
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return newId;
	}

	@Override
	public boolean update(IEntity entity) {
		boolean isSuccess = false;
		try{
			Match match = (Match)entity;
			String sql ="UPDATE "+getTableName()+
						" SET owner_id=?,competitor_id=?,winner_id=?,map_id=?,start_time=?,end_time=?,status=?"+
						" WHERE match_id=?";
			PreparedStatement statement = mConnection.prepareStatement(sql);
			statement.setInt(1, match.getOwnerId());
			statement.setInt(2, match.getCompetitorId());
			statement.setInt(3, match.getWinnerId());
			statement.setInt(4, match.getMapId());
			statement.setInt(5, match.getStartTime());
			statement.setInt(6, match.getEndTime());
			statement.setInt(7, match.getStatus());
			statement.setInt(8, match.getMatchId());
			statement.execute();
			isSuccess = statement.getUpdateCount()>0;
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return isSuccess;
	}

	@Override
	public boolean delete(int matchId) {
		boolean isSuccess = false;
		try{
			String sql ="DELETE FROM "+getTableName()+" WHERE match_id=?";
			PreparedStatement statement = mConnection.prepareStatement(sql);
			statement.setInt(1, matchId);
			statement.execute();
			isSuccess = statement.getUpdateCount()>0;
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return isSuccess;
	}

	@Override
	public boolean isExist(int matchId) {
		boolean isSuccess = false;
		try{
			String sql ="SELECT COUNT(*) FROM "+getTableName()+" WHERE match_id=?";
			PreparedStatement statement = mConnection.prepareStatement(sql);
			statement.setInt(1, matchId);
			statement.execute();
			isSuccess = statement.getUpdateCount()>0;
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return isSuccess;
	}

	@Override
	public int count() {
		int counter = 0;
		try{
			String sql ="SELECT COUNT(*) FROM "+getTableName();
			PreparedStatement statement = mConnection.prepareStatement(sql);
			statement.execute();
			counter = statement.getUpdateCount();
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return counter;
	}
	
}
